package de.fhin.mas.agents.navigator.behaviours;

import jade.core.Agent;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.FailureException;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.proto.AchieveREResponder;
import de.fhin.mas.MASEvents;
import de.fhin.mas.MASServices;
import de.fhin.mas.agents.navigator.NavigatorAgent;
import de.fhin.mas.util.MessageUtil;

/**
 * 
 * @author Matthias Wilhelm
 * 
 */
public class SystemStopRequiredHandler extends AchieveREResponder {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7793217285200761009L;

	public SystemStopRequiredHandler(Agent a) {
		super(a, MessageUtil.buildBasicCommunicationTemplate(
				FIPANames.InteractionProtocol.FIPA_REQUEST,
				MASEvents.SYSTEM_STOP_REQUIRED.getName(), ACLMessage.REQUEST,
				null));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ACLMessage prepareResponse(ACLMessage request)
			throws NotUnderstoodException, RefuseException {

		ACLMessage reply = request.createReply();
		reply.setPerformative(ACLMessage.AGREE);

		return reply;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ACLMessage prepareResultNotification(ACLMessage request,
			ACLMessage response) throws FailureException {

		((NavigatorAgent) myAgent).pause();

		ACLMessage reply = request.createReply();
		reply.setPerformative(ACLMessage.INFORM);
		try {
			reply.addReceiver(MASServices.findService(myAgent,
					MASServices.SEARCHER));
		} catch (FIPAException e) {
			e.printStackTrace();
		}
		reply.setContent("INFORM-DONE");

		return reply;
	}

}
